"""
数据库迁移：添加 signal_date 字段到 trading_signals 表
"""
from database import engine
from sqlalchemy import text

def migrate():
    """执行迁移"""
    print("="*70)
    print("📊 数据库迁移：添加 signal_date 字段")
    print("="*70)
    
    with engine.connect() as conn:
        try:
            # 添加 signal_date 字段
            sql = text("""
            ALTER TABLE trading_signals 
            ADD COLUMN signal_date DATE COMMENT '信号日期'
            """)
            conn.execute(sql)
            conn.commit()
            print("✅ signal_date 字段添加成功！")
            
            # 添加索引
            sql_index = text("""
            ALTER TABLE trading_signals 
            ADD INDEX idx_signal_date (signal_date)
            """)
            conn.execute(sql_index)
            conn.commit()
            print("✅ 索引添加成功！")
            
        except Exception as e:
            if "Duplicate column name" in str(e):
                print("⚠️ signal_date 字段已存在，跳过迁移")
            else:
                print(f"❌ 迁移失败: {e}")
                raise
    
    print("="*70)
    print("✅ 数据库迁移完成！")
    print("="*70)

if __name__ == "__main__":
    migrate()

